package com.jeect.userservice3.web;

import com.jeect.userservice3.domain.User;
import com.jeect.userservice3.service.impl.UserService;
import com.jeect.userservice3.util.ImageUtil;
import com.jeect.userservice3.util.MyPasswordEncode;
import com.jeect.userservice3.util.ResponseBody;
import com.jeect.userservice3.util.ResponseCode;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy;
import org.springframework.security.web.authentication.session.NullAuthenticatedSessionStrategy;
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;

@RestController
public class UserController {
    @Autowired
    private HttpServletRequest request;

    private ByteArrayInputStream inputStream;


//    private SessionAuthenticationStrategy sessionAuthenticationStrategy = new NullAuthenticatedSessionStrategy();
    @Autowired
    private UserService userService;
    @GetMapping("/user/get/{userid}")
    public Object setsession(@PathVariable String userid){
        HttpSession session = request.getSession();

        session.setAttribute("id",userid);
        System.out.println("sessionid  111111111111111:"+session.getId());
        return "已经设置Session id="+userid;

    }

    @GetMapping("/user/get/")
    public Object getSession(){
        HttpSession session = request.getSession();
        String id = (String)session.getAttribute("id");

        System.out.println("sessionid  222222:"+session.getId());
        return "获取的Session id="+id;
    }
    @GetMapping("/user/page/")
    public String getRandompig(HttpServletResponse response) throws IOException {
        response.setContentType("image/jpeg");
        //禁止图像缓存
        response.setHeader("Pragma","no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        HttpSession session = request.getSession();
        System.out.println("session.getId()===="+session.getId());
        ImageUtil imageUtil = new ImageUtil(120, 40, 5,30);
        session.setAttribute("code", imageUtil.getCode());
        imageUtil.write(response.getOutputStream());
        return null;
    }

    @GetMapping("/user/me/")
    public ResponseBody getCurrentUser(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            return new ResponseBody(ResponseCode.SUCCESS,authentication);

    }

    @PostMapping("/user/login")
    public ResponseBody login(HttpServletRequest request, HttpServletResponse response) {
//        PrintWriter out=response.getWriter();
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String codes = request.getParameter("charcode");
       // JSONObject json=new JSONObject();
        ResponseBody body;//= new ResponseBody();
        HttpSession session = request.getSession();
        User user = userService.getUserByUserName(username);
        BCryptPasswordEncoder encoder  = new BCryptPasswordEncoder();
        if(user!=null && "123456".equals(user.getLoginPass()) && codes.equalsIgnoreCase((String)session.getAttribute("code"))){//这里没有用数据库验证
            body = new ResponseBody(ResponseCode.SUCCESS);
            request.getSession().setAttribute("id",username);
        }else{
            body = new ResponseBody(ResponseCode.FAILE);
        }
        System.out.println(body.getMsg());
        return body;
    }

    @GetMapping("/user/loginout/")
    public ResponseBody loginout(HttpServletRequest request,HttpServletResponse response){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication!=null){
            new SecurityContextLogoutHandler().logout(request,response,authentication);
        }
        return new ResponseBody(ResponseCode.SUCCESS,"登出成功!");
    }
  /*  private void setInputStream(ByteArrayInputStream inputStream) {
        this.inputStream = inputStream;
    }

    private ByteArrayInputStream getInputStream() {
        return inputStream;
    }*/
}
